#! /usr/bin/perl -w
#
# Creates the $GENIE/src/Conventions/GBuild.h header file
#
# C.Andreopoulos <constantinos.andreopoulos \at cern.ch>, Rutherford Lab.
#

$GENIE = $ENV{'GENIE'};
die ("Not even the GENIE environmental variable is defined!") unless defined $GENIE;

# GENIE config file
#
$GCONF_FILE = "$GENIE/src/make/Make.config";
die("Can not open $GENIE/src/make/Make.config!") unless (-e $GCONF_FILE);

# Read the ROOT config
#
$ROOTSYS = $ENV{'ROOTSYS'};
$RCONF_FILE = "$ROOTSYS/config.status";
$root_conf_status="";
if(-e $RCONF_FILE) {
  open(RCONF, "<$RCONF_FILE") or die("Can not open $ROOTSYS/config.status!");
  @root_conf=<RCONF>;
  close(RCONF);
  chomp($root_conf_status = $root_conf[0]);
}

# Open header file
#
$GBLD_FILE = "$GENIE/src/Framework/Conventions/GBuild.h";
open(GBLD, ">$GBLD_FILE") or die("Can not write out the GBuild.h file!");

# Write header file
#
print GBLD "#ifndef _GBUILD_H_\n";
print GBLD "#define _GBUILD_H_\n";
print GBLD "/*\n";
print GBLD " * Build information automatically generated by the GENIE installer\n";
print GBLD "*/\n\n";

# pythia6 enabled?
#
if( $root_conf_status=~m/enable-pythia6/i )
      { print GBLD   "#define __GENIE_PYTHIA6_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_PYTHIA6_ENABLED__\n"; }

# pythia8 enabled?
#
@nret = `grep 'GOPT_ENABLE_PYTHIA8=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_PYTHIA8_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_PYTHIA8_ENABLED__\n"; }

# mysql enabled?
#
if( $root_conf_status=~m/enable-mysql/i )
      { print GBLD   "#define __GENIE_MYSQL_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_MYSQL_ENABLED__\n"; }

# flux drivers enabled?
#
@nret = `grep 'GOPT_ENABLE_FLUX_DRIVERS=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_FLUX_DRIVERS_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_FLUX_DRIVERS_ENABLED__\n"; }

# geometry drivers enabled?
#
@nret = `grep 'GOPT_ENABLE_GEOM_DRIVERS=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_GEOM_DRIVERS_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_GEOM_DRIVERS_ENABLED__\n"; }

# low-level msg printout enabled?
#
@nret = `grep 'GOPT_ENABLE_LOW_LEVEL_MESG=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_LOW_LEVEL_MESG_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_LOW_LEVEL_MESG_ENABLED__\n"; }

# VHE enabled?
#
@nret = `grep 'GOPT_ENABLE_VHE_EXTENSION=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_VHE_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_VHE_ENABLED__\n"; }

# LHAPDF5 enabled?
#
@nret = `grep 'GOPT_ENABLE_LHAPDF5=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_LHAPDF5_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_LHAPDF5_ENABLED__\n"; }

# LHAPDF6 enabled?
#
@nret = `grep 'GOPT_ENABLE_LHAPDF6=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_LHAPDF6_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_LHAPDF6_ENABLED__\n"; }

# GSL enabled?
#
@nret = `grep 'GOPT_ENABLE_GSL=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_GSL_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_GSL_ENABLED__\n"; }

# INCL enabled?
#
@nret = `grep 'GOPT_ENABLE_INCL=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_INCL_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_INCL_ENABLED__\n"; }

# Geant4 enabled?
#
@nret = `grep 'GOPT_ENABLE_GEANT4_INTERFACE=YES' $GCONF_FILE`;
if(@nret>0)
      { print GBLD   "#define __GENIE_GEANT4_INTERFACE_ENABLED__\n"; }
else  { print GBLD "//#define __GENIE_GEANT4_INTERFACE_ENABLED__\n"; }


# grab libxml2 version information
#
$libxml2_inc = "";
$ret1 = `grep GOPT_WITH_LIBXML2_INC $GCONF_FILE`;
if($ret1=~m/GOPT_WITH_LIBXML2_INC=(.+)/) {
	$libxml2_inc = $1;
}
open(VRS_FILE, "$libxml2_inc/libxml/xmlversion.h") or warn("$libxml2_inc/libxml/xmlversion.h!");
$vrs_set = 0;
foreach(<VRS_FILE>) {
	chomp $_;
#	print "$_\n";
	if(m/\Wdefine\s+LIBXML_VERSION\s+(\d+)/i) {
		$libxml2_vrs = $1;
#		print "GENIE seems to be using libxml2 version $libxml2_vrs\n";
                print GBLD   "#define __GENIE_USES_LIBXML2_VERSION__ $libxml2_vrs\n";
                $vrs_set = 1;
	}
}
if($vrs_set==0) {
        print GBLD   "//#define __GENIE_USES_LIBXML2_VERSION__ <unknown>\n";
}
close VRS_FILE;

# grab log4cpp version information
#
$libxml2_inc = "";
$ret1 = `grep GOPT_WITH_LOG4CPP_INC $GCONF_FILE`;
if($ret1=~m/GOPT_WITH_LOG4CPP_INC=(.+)/) {
	$log4cpp_inc = $1;
}

open(VRS_FILE, "$log4cpp_inc/log4cpp/config.h") or warn("$log4cpp_inc/log4cpp/config.h!");
$vrs_set = 0;
foreach(<VRS_FILE>) {
	chomp $_;
#	print "$_\n";
	if(m/\Wdefine\s+LOG4CPP_VERSION\s+.(\d+).(\d+)./i) {
	        $log4cpp_vrs = 0;
		if($1 >0 ) { $log4cpp_vrs = $1 * 100 + $2; }
#		print "GENIE seems to be using log4cpp version $log4cpp_vrs \n";
                print GBLD   "#define __GENIE_USES_LOG4CPP_VERSION__ $log4cpp_vrs\n";
                $vrs_set = 1;
	}
}
if($vrs_set==0) {
        print GBLD   "//#define __GENIE_USES_LOG4CPP_VERSION__ <unknown>\n";
}
close VRS_FILE;

print GBLD "\n#endif\n\n";

# Close header file
#
close(GBLD);
